/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package conexion;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;


/**
 *
 * @author REX
 */
public class Conexion {
    public static String peticionGET(String url){
        HttpConnection hcon = null;
        DataInputStream dis = null;
        StringBuffer respuesta = new StringBuffer();
        try{
            hcon = ( HttpConnection )Connector.open( url );
            dis = new DataInputStream( hcon.openInputStream() );
            int c;
            while( (c = dis.read()) != -1 ){
                respuesta.append( (char)c );
            }
        }
        catch(Exception e){
            e.printStackTrace();
            respuesta.append( "" );

        }
        finally{
            try{
                if ( hcon != null ) hcon.close();
                if ( dis != null ) dis.close();
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
        }
        return respuesta.toString();
    }
    /**
     *@param url : La pagina web a la que se le hara la peticion, sin parametros
     *@param param : El nombre del parametro
     *@param valor : el valor del parametro
     *
     *
     */
    public static String peticionGETconParametro(String url, String param, int valor){
        HttpConnection hcon = null;
        DataInputStream dis = null;
        StringBuffer respuesta = new StringBuffer();
        url.concat("?"+param+"="+valor);
        try{
            hcon = ( HttpConnection )Connector.open( url );
            dis = new DataInputStream( hcon.openInputStream() );
            int c;
            while( (c = dis.read()) != -1 ){
                respuesta.append( (char)c );
            }
        }
        catch(Exception e){
            e.printStackTrace();
            respuesta.append( "" );

        }
        finally{
            try{
                if ( hcon != null ) hcon.close();
                if ( dis != null ) dis.close();
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
        }
        return respuesta.toString();
    }
    /*
     *Funcion echa para los boletos
     */
    public static Image cargaImagen(String url){
        HttpConnection hcon = null;
        DataInputStream dis = null;
        ByteArrayOutputStream bStrm = null;
        Image img = null;
        try{
            byte[] data;
            hcon = (HttpConnection) Connector.open(url);
            dis = hcon.openDataInputStream();
            int length = (int) hcon.getLength();
            if (length != -1){
                data = new byte[length];
                dis.readFully(data);
            }
            else{
                bStrm = new ByteArrayOutputStream();
                int ch;
                while ((ch = dis.read()) != -1)
                  bStrm.write(ch);
                data = bStrm.toByteArray();
                bStrm.close();
            }
            img =Image.createImage(data, 0, data.length);
         }catch(Exception e){
             e.printStackTrace();
         }
        finally{
            try{
                if ( hcon != null ) hcon.close();
                if ( dis != null ) dis.close();
                }catch(IOException ioe){
                    ioe.printStackTrace();
                }
            }
         return (img == null ? null : img);
    }
    
}
